package com.herui.iot.ui.evaluation.execute;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import butterknife.BindView;
import butterknife.OnClick;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.herui.sdyu_lib.utils.ToastUtil;
import com.herui.sport_care.R;
import com.herui.sport_care.data.check.DeviceInfo;
import com.herui.sport_care.data.eval.EvalInfo;
import com.herui.sport_care.request.SortType;
import com.herui.sport_care.ui.BaseSportFragment;
import com.herui.sport_care.utils.ScreenUtils;
import com.herui.sport_care.utils.ViewTools;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link AskQuesFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class AskQuesFragment extends BaseSportFragment {

    @BindView(R.id.view_question)
    LinearLayout view_question;

    private EvalInfo evalInfo;
    private ExecteServerPresenter execteServerPresenter;

    private String[] items = {"非常轻松", "很轻松", "轻松", "稍费力", "费力", "很费力", "非常费力"};
    private CheckBox[] checkBoxes;

    public static AskQuesFragment newInstance(EvalInfo evalInfo) {
        AskQuesFragment fragment = new AskQuesFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        fragment.evalInfo = evalInfo;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        execteServerPresenter = new ExecteServerPresenter(view);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        setTitle("自觉最大用力");
        return view;
    }

    @Override
    public int getLayout() {
        return R.layout.fragment_ask_ques;
    }

    @Override
    public void initParam() {

        checkBoxes = new CheckBox[items.length];

        LinearLayout.LayoutParams params =
                new LinearLayout.LayoutParams((int) ViewTools.fromDpToPx(150),
                        ViewGroup.LayoutParams.WRAP_CONTENT);

        LayoutInflater inflater = LayoutInflater.from(baseActivity);
        for (int i = 0; i < items.length; i++) {
            View layout = inflater.inflate(R.layout.item_ask_question, null);
            view_question.addView(layout, params);

            TextView textView = layout.findViewById(R.id.tv_title);
            textView.setText(items[i]);
            CheckBox checkBox = layout.findViewById(R.id.checkbox);
            checkBoxes[i] = checkBox;
            int finalI = i;
            checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked){
                        for (int j = 0; j < items.length; j++) {
                            if (finalI != j){
                                checkBoxes[j].setChecked(false);
                            }
                        }
                    }
                }
            });
        }
    }

    @OnClick({R.id.btn_ok})
    public void onViewClick(View view) {
        switch (view.getId()) {
            case R.id.btn_ok:
                boolean flag = false;
                for (int i = 0; i < checkBoxes.length; i++) {
                    if (checkBoxes[i].isChecked()){
                        execteServerPresenter.uploadRpe(evalInfo.getEvaluationSchemeId(), items[i]);
                        flag = true;
                        break;
                    }
                }
                if (!flag){
                    baseActivity.popTo(EvalChoseFragment.class, false);
                }
                break;
            default:
                break;
        }

    }

    ExecteServerPresenter.View view = new ExecteServerPresenter.View() {
        @Override
        public void onUploadRpeSucess() {
            baseActivity.popTo(EvalChoseFragment.class, false);
        }
    };

    public static interface FragmentLis{
        void onResult(String s);
    }
}